<?php
/**
 *@file commenttype.php
 *@author LL
 *@date 2015-7-10 
 */
namespace haibao\comment\business;

use \haibao\comment\data\CommentType as CommentTypeData;
use \haibao\comment\business\BusinessException;
use \haibao\frame\data\query\Condition ;
use \haibao\comment\model\filter\CommentType as CommentTypeFilter;
use \haibao\comment\model\data\CommentType as CommentTypeModel;

class CommentType extends \haibao\comment\business\Base{
    
    public static function addCommentType($model){
		\haibao\cms\business\Base::checkOperation(self::FUNC_CODE_COMMENTTYPE_ADD);

		self::checkModelData($model);

		$commentTypeData = new CommentTypeData();
		$commentTypeData->add($model);
		
		\haibao\cms\business\Base::saveOperateHistory(self::FUNC_CODE_COMMENTTYPE_ADD, $model->toArray(false));
	}
	
	public static function updateCommentType($model){
		\haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COMMENTTYPE_EDIT);	

		self::checkModelData($model,true);
		$commentTypeData = new CommentTypeData();
		$commentTypeData->updateById($model->Id, $model);
		\haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COMMENTTYPE_EDIT, $model->toArray(false));
	}

	public static function delCommentType($id,$arrSet){
		\haibao\cms\business\Base::checkOperation(self::FUNC_CODE_COMMENTTYPE_DEL);
		if (!$id){
			throw new BusinessException('类型Id不能为空');
		}
		$commentTypeData = new CommentTypeData();
		$commentTypeData->updateById($id,$arrSet);
		\haibao\cms\business\Base::saveOperateHistory(self::FUNC_CODE_COMMENTTYPE_DEL, array($id));
	}
	
	public static function getModelById($commentTypeId){
		if(!$commentTypeId){
			return NULL;
		}
		$commentTypeData = new CommentTypeData();
		return $commentTypeData->getOneById($commentTypeId);
	}
	
    public static function getCommentTypeList($commentTypeFilter){
        $commentTypeData = new CommentTypeData();
        return $commentTypeData->getAll($commentTypeFilter);
    }
    
    /**
     * 根据站点名字获取站点信息
     * @param : String $commentTypeName
     * @param : Int $commentTypeId
     */
    public static function getByName($commentTypeName, $commentTypeId = null){
        $data = new CommentTypeData;
        return $data->getByName($commentTypeName,$commentTypeId);
    
    }
    public static function getByCode($commentTypeCode, $commentTypeId = null){
        $data = new CommentTypeData;
        return $data->getByCode($commentTypeCode,$commentTypeId);
    
    }
    
    private static function checkModelData($model,$isUpdate = false){

    	if($isUpdate) {	
			if(!$model->Id){
				throw new BusinessException('类型Id不能为空');
			}
    	}
    	if(!$model->TypeName){
    		throw new BusinessException('请添加类型');
    	}
    	if(!$model->TypeCode){
    	    throw new BusinessException('请添加编号');
    	}
    	if(!$model->TypeDesc){
    		throw new BusinessException('请添加描述');
    	}
    	

    	self::checkCommentTypeCodeIsExist($model,$isUpdate);
    }
    
	private static function checkCommentTypeCodeIsExist($model,$isUpdate = false){
		
		$filter = new CommentTypeFilter();
		if($isUpdate){
			$filter->where(CommentTypeFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTEQUAL,$model->Id);
			$filter->where(CommentTypeFilter::CONDITION_FIELD_NAME_TYPE_CODE,Condition::CONDITION_EQUAL,$model->TypeCode);
			$filter->where(CommentTypeFilter::CONDITION_FIELD_NAME_TYPE_STATUS,Condition::CONDITION_EQUAL,CommentTypeModel::STATUS_OK);
		}else{
			$filter->where(CommentTypeFilter::CONDITION_FIELD_NAME_TYPE_CODE,Condition::CONDITION_EQUAL,$model->TypeCode);
			$filter->where(CommentTypeFilter::CONDITION_FIELD_NAME_TYPE_STATUS,Condition::CONDITION_EQUAL,CommentTypeModel::STATUS_OK);
		}
		 
		$commentTypeData = new CommentTypeData();
		if($commentTypeData->getOne($filter)){
			throw new BusinessException('编号必须唯一',\haibao\cms\business\Base::DATA_ERROR_COMMENT_TYPE_CODE_EXIST);
		}
		
		$filter = new CommentTypeFilter();
		if($isUpdate){
		    $filter->where(CommentTypeFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTEQUAL,$model->Id);
		    $filter->where(CommentTypeFilter::CONDITION_FIELD_NAME_TYPE_NAME,Condition::CONDITION_EQUAL,$model->TypeName);
		    $filter->where(CommentTypeFilter::CONDITION_FIELD_NAME_TYPE_STATUS,Condition::CONDITION_EQUAL,CommentTypeModel::STATUS_OK);
		}else{
		    $filter->where(CommentTypeFilter::CONDITION_FIELD_NAME_TYPE_NAME,Condition::CONDITION_EQUAL,$model->TypeName);
		    $filter->where(CommentTypeFilter::CONDITION_FIELD_NAME_TYPE_STATUS,Condition::CONDITION_EQUAL,CommentTypeModel::STATUS_OK);
		}
			
		$commentTypeData = new CommentTypeData();
		if($commentTypeData->getOne($filter)){
		    throw new BusinessException('类型名必须唯一',\haibao\cms\business\Base::DATA_ERROR_COMMENT_TYPE_NAME_EXIST);
		}
	}
}